package com.demo.datastructure.tree;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @description: letcode 102 二叉树的层序遍历
 * @author: ljr
 * @time: 2021/5/23 14:34
 */
public class TreeIteratorDemo1 {

     private static class TreeNode {
         Integer val;
         TreeNode left;
         TreeNode right;

         public TreeNode() { }

         TreeNode(int val) { this.val = val; }
         public TreeNode(int val, TreeNode left, TreeNode right) {
             this.val = val;
             this.left = left;
             this.right = right;
         }
     }

    public List<List<Integer>> levelOrder(TreeNode root) {
        if(root == null || root.val == 0){ return  new ArrayList<>(); }
        List<List<Integer>> intList = new ArrayList<>();
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        while(!deque.isEmpty()){
            List<Integer> list = new ArrayList<>();
            int treeSize = deque.size();
            for(int i = 0; i < treeSize; i++){
                TreeNode rootNode = deque.pop();
                if(rootNode.val != 0){
                    list.add(rootNode.val);
                }

                if(rootNode.left != null){
                    deque.offer(rootNode.left);
                }

                if(rootNode.right != null){
                    deque.offer(rootNode.right);
                }
            }
            intList.add(list);
        }
        return intList;
    }

    private void iteratorTree(List<List<Integer>> intList, Deque<TreeNode> nodeList) {
        if(nodeList.isEmpty()){ return; }
        List<Integer> list = new ArrayList<>();
        int i = 0;
        TreeNode node = null;
        while(!nodeList.isEmpty()){
            if(i > 0 && nodeList.peek() == node){
                break;
            }

            TreeNode rootNode = nodeList.removeFirst();
            if(rootNode.val != 0){
                list.add(rootNode.val);
            }

            if(rootNode.left != null){
                nodeList.addLast(rootNode.left);
                if(i == 0){
                   i++;
                   node = rootNode.left;
                }
            }

            if(rootNode.right != null){
                nodeList.addLast(rootNode.right);
                if(i == 0){
                    i++;
                    node = rootNode.right;
                }
            }
        }
        intList.add(list);
        iteratorTree(intList,nodeList);
    }

    void inorder(TreeNode root){

    }


    public static void main(String[] args) {
        TreeNode rootNode = new TreeNode(3);
        TreeNode rootNode1 = new TreeNode(9);
        TreeNode rootNode2 = new TreeNode(20);
        rootNode.left = rootNode1;
        rootNode.right = rootNode2;
        TreeNode rootNode4 = new TreeNode(15);
        TreeNode rootNode5 = new TreeNode(7);
        rootNode2.left = rootNode4;
        rootNode2.right = rootNode5;
        TreeIteratorDemo1 t = new TreeIteratorDemo1();
        System.out.println(t.levelOrder(rootNode));
    }

}
